/**
 * 
 */
package at.ac.tuwien.swa075.telco.hlr.dao.hibernate;

import java.util.List;

import org.hibernate.Transaction;

import at.ac.tuwien.swa075.telco.hlr.HlrException;
import at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO;
import at.ac.tuwien.swa075.telco.hlr.dao.entity.CustomerEntity;

/**
 * @author vovmj01
 * 
 */
public class HibernateCustomerDAO implements ICustomerDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#deleteCustomer(int)
	 */
	public boolean deleteCustomer(int customerId) throws HlrException {
		CustomerEntity customer = findCustomer(customerId);
		if (customer == null) {
			return false;
		}
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		HibernateSessionFactory.getInstance().getCurrentSession().delete(customer);
		transcation.commit();
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#findCustomerByEquipment(java.lang.String)
	 */
	public CustomerEntity findCustomerByEquipment(String imei) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		CustomerEntity customer = (CustomerEntity) HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM CustomerEntity as h WHERE imei = '" + imei + "'")
				.uniqueResult();
		transcation.commit();
		return customer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#findCustomerBySubscription(java.lang.String)
	 */
	public CustomerEntity findCustomerBySubscription(String imsi) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		CustomerEntity customer = (CustomerEntity) HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM CustomerEntity as h WHERE imsi = '" + imsi + "'")
				.uniqueResult();
		transcation.commit();
		return customer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#insertCustomer(at.ac.tuwien.swa075.telco.hlr.dao.entities.CustomerEntity)
	 */
	public int insertCustomer(CustomerEntity customer) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		HibernateSessionFactory.getInstance().getCurrentSession().save(customer);
		transcation.commit();
		return customer.getId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#selectCustomers(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public List<CustomerEntity> selectCustomers(String firstname, String lastname, String birthDate) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		List<CustomerEntity> customers = HibernateSessionFactory.getInstance().getCurrentSession().createQuery(
				"FROM CustomerEntity WHERE firstname = '" + firstname + "' AND lastname = '" +  lastname +"' AND birthDate = '" + birthDate + "'").list();
		transcation.commit();
		return customers;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#selectCustomersByStatus(java.lang.String)
	 */
	public List<CustomerEntity> selectCustomersByStatus(String status) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		List customers = HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM CustomerEntity as h WHERE status = " + status + "").list();
		transcation.commit();
		return customers;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ICustomerDAO#updateCustomer(at.ac.tuwien.swa075.telco.hlr.dao.entities.CustomerEntity)
	 */
	public boolean updateCustomer(CustomerEntity customer) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		HibernateSessionFactory.getInstance().getCurrentSession().update(customer);
		transcation.commit();
		return true;
	}

	public CustomerEntity findCustomer(int customerId) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		CustomerEntity customer = (CustomerEntity) HibernateSessionFactory.getInstance().getCurrentSession().get(CustomerEntity.class, Integer.valueOf(customerId));
		transcation.commit();
		return customer;
	}

}
